/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-03-20 Marco Riccardi - initial implementation
 *
 */
package org.mizartools.system.executable;

import org.mizartools.utility.IOutputAnalyzer;
import org.mizartools.utility.TemporaryDirectory;

public class IrrelevantPremisesDetector extends AbstractExecutableFile {

	private int lineNumberParser = 0;
	private int lineNumberAnalyzer = 0;
	private int lineNumberChecker = 0;
	
	public IrrelevantPremisesDetector(TemporaryDirectory temporaryDirectory, String mizarFile)
			throws ExecutableException {
		super(temporaryDirectory, mizarFile);
		this.executableFileEnum = ExecutableFileEnum.relprem;
		this.outputAnalyzer = new Analyzer();
	}

	@Override
	public int getPercentage() {
		if (!isExecuting()) return 100;
		if (getLineNumberParser() > 0) {
			return (int) ((getLineNumberAnalyzer()*10+getLineNumberChecker()*90)/getLineNumberParser());
		} 
		return 0;
	}

	private class Analyzer implements IOutputAnalyzer {

		@Override
		public void analyze(String line) {
			int i = getInteger(line);
			if (line.startsWith("Parser")) {
				if (getLineNumberParser() < i) setLineNumberParser(i);
			} else {
				if (line.startsWith("Analyzer")) {
					if (getLineNumberAnalyzer() < i) setLineNumberAnalyzer(i);
				} else {
					if (line.startsWith("Checker")) {
						if (getLineNumberChecker() < i) setLineNumberChecker(i);
					}
				}
			}
		}
		
		private int getInteger(String line) {
			int i = -1;
			int index = line.indexOf("[");
			if (index == -1) return i;
			String s = line.substring(index+1);
			index = s.indexOf("]");
			if (index == -1) return i;
			s = s.substring(0,index);
			try {
				i = Integer.parseInt(s);
			} catch (NumberFormatException e) {}
			return i;
		}
		
	}

	public int getLineNumber() {
		return getLineNumberParser();
	}

	private void setLineNumberParser(int lineNumberParser) {
		synchronized (this) {
			this.lineNumberParser = lineNumberParser;
		}
	}

	private int getLineNumberParser() {
		synchronized (this) {
			return lineNumberParser;
		}
	}

	private void setLineNumberAnalyzer(int lineNumberAnalyzer) {
		synchronized (this) {
			this.lineNumberAnalyzer = lineNumberAnalyzer;
		}
	}

	private int getLineNumberAnalyzer() {
		synchronized (this) {
			return lineNumberAnalyzer;
		}
	}

	private void setLineNumberChecker(int lineNumberChecker) {
		synchronized (this) {
			this.lineNumberChecker = lineNumberChecker;
		}
	}

	private int getLineNumberChecker() {
		synchronized (this) {
			return lineNumberChecker;
		}
	}
	
}
